import os

from google.appengine.api import users
from django import shortcuts
from django.http import HttpResponse, HttpResponseForbidden

isDev = 'Dev' in os.getenv('SERVER_SOFTWARE')
def respond(request, user, template, params=None):
    """Helper to render a response, passing standard stuff to the response.

    Args:
      request: The request object.
    user: The User object representing the current user; or None if nobody
      is logged in.
    params: A dict giving the template parameters; modified in-place.

    Returns:
      Whatever render_to_response(template, params) returns.

    Raises:
      Whatever render_to_response(template, params) raises.
    """
    if params is None:
        params = {}
    if user:
        params['user'] = user
        params['nickname'] = user.nickname()
        params['sign_out'] = users.CreateLogoutURL(request.path)
        params['is_admin'] = (users.IsCurrentUserAdmin() and isDev)
    else:
        params['sign_in'] = users.CreateLoginURL(request.path)
        
    return shortcuts.render_to_response(template, params)

def login(request):
    user = users.GetCurrentUser()
    if user is None:
        return respond(request, user, 'login.html', None)
    return respond(request, user, 'home.html', None)


def forbidden(request, users, reason):
    return HttpResponseForbidden(reason)


def done(request, user, msg):
    return HttpResponse(msg)


